package org.gdstash.ui.select;

import org.gdstash.db.SelectionCriteria;
import org.gdstash.ui.GDStashFrame;
import org.gdstash.ui.util.AdjustablePanel;
import org.gdstash.util.GDMsgFormatter;

import javax.swing.*;
import javax.swing.border.Border;
import javax.swing.border.TitledBorder;
import java.awt.*;

public class DamageSelectionPane extends AdjustablePanel {
    private DamageClassPane pnlDmgClass;
    private DamageFlatPercPane pnlFlatPerc;
    private DamageFlatPane pnlFlat;
    private DamageDoTPane pnlDoT;
    private DamageSpecialPane pnlSpecial;
    private DamageCrowdControl pnlCrowd;

    public DamageSelectionPane() {
        this.adjustUI();
        final JPanel pnlDmgTypes = new JPanel();
        GroupLayout layout = null;
        GroupLayout.SequentialGroup hGroup = null;
        GroupLayout.SequentialGroup vGroup = null;
        layout = new GroupLayout(pnlDmgTypes);
        pnlDmgTypes.setLayout(layout);
        layout.setAutoCreateGaps(false);
        layout.setAutoCreateContainerGaps(false);
        hGroup = layout.createSequentialGroup();
        hGroup.addGroup(layout.createParallelGroup().addComponent(this.pnlFlat)).addGroup(layout.createParallelGroup().addComponent(this.pnlDoT)).addGroup(layout.createParallelGroup().addComponent(this.pnlSpecial)).addGroup(layout.createParallelGroup().addComponent(this.pnlCrowd));
        layout.setHorizontalGroup(hGroup);
        vGroup = layout.createSequentialGroup();
        vGroup.addGroup(layout.createParallelGroup().addComponent(this.pnlFlat).addComponent(this.pnlDoT).addComponent(this.pnlSpecial).addComponent(this.pnlCrowd));
        layout.setVerticalGroup(vGroup);
        layout.linkSize(0, this.pnlFlat, this.pnlDoT);
        layout.linkSize(0, this.pnlFlat, this.pnlSpecial);
        layout.linkSize(0, this.pnlFlat, this.pnlCrowd);
        layout.linkSize(1, this.pnlFlat, this.pnlDoT);
        layout.linkSize(1, this.pnlFlat, this.pnlSpecial);
        layout.linkSize(1, this.pnlFlat, this.pnlCrowd);
        layout = new GroupLayout(this);
        this.setLayout(layout);
        layout.setAutoCreateGaps(false);
        layout.setAutoCreateContainerGaps(false);
        hGroup = layout.createSequentialGroup();
        hGroup.addGroup(layout.createParallelGroup().addComponent(this.pnlDmgClass).addComponent(this.pnlFlatPerc).addComponent(pnlDmgTypes));
        layout.setHorizontalGroup(hGroup);
        vGroup = layout.createSequentialGroup();
        vGroup.addGroup(layout.createParallelGroup().addComponent(this.pnlDmgClass)).addGroup(layout.createParallelGroup().addComponent(this.pnlFlatPerc)).addGroup(layout.createParallelGroup().addComponent(pnlDmgTypes));
        layout.setVerticalGroup(vGroup);
        layout.linkSize(0, this.pnlDmgClass, this.pnlFlatPerc);
        layout.linkSize(0, this.pnlDmgClass, pnlDmgTypes);
    }

    @Override
    public void adjustUI() {
        Font fntLabel = UIManager.getDefaults().getFont("Label.font");
        Font fntBorder = UIManager.getDefaults().getFont("TitledBorder.font");
        if (fntBorder == null) {
            fntBorder = fntLabel;
        }
        fntLabel = fntLabel.deriveFont((float) GDStashFrame.iniConfig.sectUI.fontSize);
        fntBorder = fntBorder.deriveFont((float) GDStashFrame.iniConfig.sectUI.fontSize);
        final Border lowered = BorderFactory.createEtchedBorder(1);
        final Border raised = BorderFactory.createEtchedBorder(0);
        final Border compound = BorderFactory.createCompoundBorder(raised, lowered);
        final TitledBorder text = BorderFactory.createTitledBorder(compound, GDMsgFormatter.getString(GDMsgFormatter.rbGD, "TXT_DAMAGE_INFO"));
        text.setTitleFont(fntBorder);
        this.setBorder(text);
        if (this.pnlDmgClass == null) {
            this.pnlDmgClass = new DamageClassPane(0);
        } else {
            this.pnlDmgClass.adjustUI();
        }
        if (this.pnlFlatPerc == null) {
            this.pnlFlatPerc = new DamageFlatPercPane(0);
        } else {
            this.pnlFlatPerc.adjustUI();
        }
        if (this.pnlFlat == null) {
            this.pnlFlat = new DamageFlatPane();
        } else {
            this.pnlFlat.adjustUI();
        }
        if (this.pnlDoT == null) {
            this.pnlDoT = new DamageDoTPane();
        } else {
            this.pnlDoT.adjustUI();
        }
        if (this.pnlSpecial == null) {
            this.pnlSpecial = new DamageSpecialPane();
        } else {
            this.pnlSpecial.adjustUI();
        }
        if (this.pnlCrowd == null) {
            this.pnlCrowd = new DamageCrowdControl();
        } else {
            this.pnlCrowd.adjustUI();
        }
    }

    public void clear() {
        this.pnlDmgClass.clear();
        this.pnlFlatPerc.clear();
        this.pnlFlat.clear();
        this.pnlDoT.clear();
        this.pnlSpecial.clear();
        this.pnlCrowd.clear();
    }

    public void addCriteria(final SelectionCriteria criteria) {
        this.pnlDmgClass.addCriteria(criteria);
        this.pnlFlatPerc.addCriteria(criteria);
        this.pnlFlat.addCriteria(criteria);
        this.pnlDoT.addCriteria(criteria);
        this.pnlSpecial.addCriteria(criteria);
        this.pnlCrowd.addCriteria(criteria);
        criteria.selMode = SelectionCriteria.SelectionMode.ITEM;
    }
}
